package org.hdz.changelanguage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

/**
 *
 * @author hdz
 * @date 2019/1/5
 */

public class LanguageAdapter extends BaseAdapter {

    private Context context;
    private List<String> langList;
    private int checked = -1;

    public LanguageAdapter(Context context, List<String> langList) {
        this.context = context;
        this.langList = langList;
    }

    public void setChecked(int index) {
        this.checked = index;
    }

    @Override
    public int getCount() {
        return langList.size();
    }

    @Override
    public Object getItem(int position) {
        return langList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.language_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvLangName = (TextView) convertView.findViewById(R.id.languageName);
            viewHolder.imgIsChoiced = (ImageView) convertView.findViewById(R.id.imgItemSelect);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvLangName.setText(langList.get(position));

        if (position == checked) {
            viewHolder.imgIsChoiced.setImageResource(R.drawable.checked);
        } else {
            viewHolder.imgIsChoiced.setImageResource(R.drawable.unchecked);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView tvLangName;
        ImageView imgIsChoiced;
    }
}
